<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>秒杀配置</title>
    <link rel="stylesheet" href="{__CSS__}/element_ui.css"/>
    <link rel="stylesheet" href="{__CSS__}/style.css"/>
    <script src="{__JS__}/vue2.js"></script>
    <script src="{__JS__}/element_ui.js"></script>
    <script src="{__JS__}/axios.min.js"></script>
    <script src="{__JS__}/request.js"></script>
</head>
<body>
<div id="app">
    <div class="app-loading" v-if="pageLoading">
        <div class="app-loading__logo">
            <img src="{__IMG__}/logo.png"/>
        </div>
        <div class="app-loading__loader"></div>
        <div class="app-loading__title">SparkShop</div>
    </div>
    <el-card class="box-card" v-else>
        <div slot="header" class="clearfix">
            <span>秒杀配置</span>
        </div>
        <el-button type="primary" icon="el-icon-plus" size="small" @click="addAttr" style="margin-top: 10px">添加参数</el-button>
        <div style="width:100%;height:0;border-bottom:#E4E7ED 1px dashed;margin-top: 15px"></div>
        <el-table
                :data="tableData"
                style="width: 100%;">
            <el-table-column
                    type=""
                    prop="id"
                    label="ID">
            </el-table-column>
            <el-table-column
                    prop="start_hour"
                    label="开始时间">
            </el-table-column>
            <el-table-column
                    prop="continue_hour"
                    label="持续时长(小时)">
            </el-table-column>
            <el-table-column
                    label="状态">
                <template slot-scope="scope">
                    <el-tag type="success" v-if="scope.row.status == 1">启用</el-tag>
                    <el-tag type="danger" v-if="scope.row.status == 2">禁用</el-tag>
                </template>
            </el-table-column>
            <el-table-column
                    prop="sort"
                    label="排序">
            </el-table-column>
            <el-table-column
                    prop="operation"
                    label="操作">
                <template slot-scope="scope">
                    <el-button @click="handleEdit(scope.row)" type="text" size="small">编辑</el-button>
                    <el-button @click="handleDel(scope.row)" type="text" size="small">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination
                style="margin-top: 10px"
                background
                layout="->, prev, pager, next"
                :page-size="searchForm.limit"
                @current-change="handlePageChange"
                :total="total">
        </el-pagination>

        <el-dialog :title="title" :visible.sync="dialogVisible" width="700px">
            <el-form :model="form" :rules="rules" ref="ruleForm"  label-width="80px">
                <el-form-item label="开启整点" prop="start_hour">
                    <el-input v-model="form.start_hour" type="number"></el-input>
                </el-form-item>
                <el-form-item label="持续时长" prop="continue_hour">
                    <el-input v-model="form.continue_hour" type="number"></el-input>
                </el-form-item>
                <el-form-item label="是否有效" prop="status">
                    <el-radio :label="1" v-model="form.status">有效</el-radio>
                    <el-radio :label="2" v-model="form.status">无效</el-radio>
                </el-form-item>
                <el-form-item label="排序" prop="sort">
                    <el-input v-model="form.sort" type="number"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false" size="small">取 消</el-button>
                <el-button type="primary" @click="optSubmit('ruleForm')" size="small" :loading="loading">确 定</el-button>
            </div>
        </el-dialog>
    </el-card>
</div>

<script>
    new Vue({
        el: '#app',
        data: function () {
            return {
                pageLoading: true,
                searchForm: {
                    limit: 15,
                    page: 1
                },
                dialogVisible: false,
                title: '添加时间段',
                total: 1,
                tableData: [],
                dialogCateVisible: false,
                form: {
                    id: 0,
                    start_hour: '',
                    continue_hour: '',
                    status: 1,
                    sort: 0
                },
                rules: {
                    start_hour: [
                        { required: true, message: '请输入开启整点', trigger: 'blur' }
                    ],
                    continue_hour: [
                        { required: true, message: '请输入持续时长', trigger: 'blur' }
                    ],
                    status: [
                        { required: true, message: '请选择状态', trigger: 'blur' }
                    ],
                    sort: [
                        { required: true, message: '请输入排序值', trigger: 'blur' }
                    ]
                },
                loading: false
            }
        },
        mounted() {
            this.getList()
            this.pageLoading = false
        },
        methods: {
            // 获取列表
            async getList() {
                let res = await request.get('/addons/seckill/admin.seckillTime/index', this.searchForm)
                this.tableData = res.data.data
                this.total = res.data.total
            },
            // 初始化表单
            initForm() {
                this.form = {
                    id: 0,
                    start_hour: '',
                    continue_hour: '',
                    status: 1,
                    sort: 0
                }
            },
            // 添加参数
            addAttr() {
                this.title = '添加时间段'
                this.initForm()
                this.dialogVisible = true
            },
            // 搜索
            onSubmit() {
                this.searchForm.page = 1
                this.getList()
            },
            // 编辑参数
            handleEdit(row) {
                this.title = '编辑时间段'
                this.dialogVisible = true

                this.form = row
                delete this.form.status_txt
            },
            // 删除参数
            handleDel(item) {
                this.$confirm('此操作将永久删除该时间段, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(async () => {
                    let res = await request.get('/addons/seckill/admin.seckillTime/del', {id: item.id})
                    if (res.code == 0) {
                        this.$message.success(res.msg)
                        this.getList()
                    } else {
                        this.$message.error(res.msg)
                    }
                }).catch(() => {});
            },
            // 分页
            handlePageChange(page) {
                this.searchForm.page = page
                this.getList()
            },
            // 新增或编辑
            optSubmit(formName) {
                this.$refs[formName].validate(async (valid) => {
                    if (valid) {
                        this.loading = true
                        let res;
                        if (this.form.id) {
                            res = await request.post('/addons/seckill/admin.seckillTime/edit', this.form)
                        } else {
                            res = await request.post('/addons/seckill/admin.seckillTime/add', this.form)
                        }
                        this.loading = false
                        if (res.code == 0) {
                            this.dialogVisible = false
                            this.$message.success(res.msg)
                            this.getList()
                        } else {
                            this.$message.error(res.msg)
                        }
                    }
                });
            }
        }
    });
</script>
<style>
    .el-table__header tr,
    .el-table__header th {
        padding: 0;
        height: 20px;
        line-height: 20px;
    }
    p { padding: 0;
        margin: 0;}
</style>
</body>
</html>